package com.lvtongka.app.common

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.net.Uri
import android.os.Environment
import android.util.Base64
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Date


class FileUtils {
    companion object {
        /**
         * 将图片文件读取为字节数组。
         * 使用Base64类将字节数组编码为Base64字符串。
         */
        fun compressImageAndGetBase64String(imagePath: String): String? {
            try {
                // 解码图片获取位图
                val options = BitmapFactory.Options()
                options.inSampleSize = 2 // 图片压缩比例
                val bitmap = BitmapFactory.decodeFile(imagePath, options)
                // 将位图压缩成字节数组
                val byteArrayOutputStream = ByteArrayOutputStream()
                bitmap.compress(
                    Bitmap.CompressFormat.JPEG,
                    70,
                    byteArrayOutputStream
                )
                // 压缩方式，质量和输出流
                val byteArray = byteArrayOutputStream.toByteArray()
                // 将字节数组转换为Base64编码的字符串
                val base64String = Base64.encodeToString(byteArray, Base64.DEFAULT)
                // 释放资源
                bitmap.recycle()
                byteArrayOutputStream.close()
                return base64String
            } catch (e: Exception) {
                e.printStackTrace()
                return null
            }
        }

        @Throws(IOException::class)
        fun compressImageFromUri(context: Context, imageUri: Uri?): String {
            var inputStream = context.contentResolver.openInputStream(imageUri!!)
            val options = BitmapFactory.Options()
            options.inJustDecodeBounds = true
            BitmapFactory.decodeStream(inputStream, null, options)
            inputStream!!.close()

            options.inJustDecodeBounds = false
            options.inSampleSize = 2
            inputStream = context.contentResolver.openInputStream(imageUri)

            val bitmap = BitmapFactory.decodeStream(inputStream, null, options)
            inputStream!!.close()
//            val compressedBitmap =
//                Bitmap.createBitmap(bitmap!!.width, bitmap.height, Bitmap.Config.ARGB_8888)
            bitmap?.recycle()
            val byteArrayOutputStream = ByteArrayOutputStream()
            bitmap?.compress(
                Bitmap.CompressFormat.JPEG,
                70,
                byteArrayOutputStream
            )
            // 压缩方式，质量和输出流
            val byteArray = byteArrayOutputStream.toByteArray()
            val base64String = Base64.encodeToString(byteArray, Base64.DEFAULT)
            byteArrayOutputStream.close()
            return base64String
        }

        @SuppressLint("SimpleDateFormat")
        fun createImageFile(context: Context): File? {
            // 创建一个图片文件
            val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
            val imageFileName = "JPEG_" + timeStamp + "_"
            val storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
            var image: File? = null
            try {
                image = File.createTempFile(
                    imageFileName,  /* prefix */
                    ".jpg",  /* suffix */
                    storageDir /* directory */
                )
            } catch (e: IOException) {
                // 错误处理
            }
            return image
        }
    }

}